<template>
    <transition name="fade">
        <div class="brs-popup" v-if="open">
            <div class="brs-popup__mask" :style=" 'opacity: ' + (mask ? '0.6' : '0')" @click="maskClick"></div>
            <div class="brs-popup__wrapper">
                <div class="brs-popup__header" v-if="showHeader">
                    <div class="brs-popup__cancel" @click="cancel">取消</div>
                    <div class="brs-popup__title">{{title}}</div>
                    <div class="brs-popup__confirm" @click="confirm">确认</div>
                </div>
                <div class="brs-popup__body">
                    <slot></slot>
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
export default {
    name: 'brs-popup',
    props: {
        open: {
            type: Boolean,
            default: false
        },
        mask: {
            type: Boolean,
            default: true
        },
        showHeader: {
            type: Boolean,
            default: true
        },
        title: {
            type: String,
            default: ''
        }
    },
    methods: {
        // 遮罩层点击
        maskClick(){
            if(this.mask){
                this.cancel()
            }else if(!this.showHeader){
                this.cancel()
            }
        },
        // 取消
        cancel(){
            this.$emit('update:open', false);
            this.$emit('cancel');
        },
        // 确认
        confirm(){
            this.$emit('update:open', false);
            this.$emit('confirm');
        },
    }
}
</script>

<style lang="less" scoped>
.fade-enter-active, .fade-leave-active {
    transition: opacity .2s;
    .brs-popup__wrapper{
        animation: moveIn 0.3s;
    }
}
.fade-enter, .fade-leave-to, .fade-leave-active {
    opacity: 0;
    .brs-popup__wrapper{
        animation: moveOut 0.3s;
    }
}
@keyframes moveIn {
    0% {
        transform: translateY(100%);
    }
    100% {
        transform: translateY(0%);
        animation-fill-mode: forwards
    }
}
@keyframes moveOut {
    0% {
        transform: translateY(0%);
    }
    100% {
        transform: translateY(100%);
        animation-fill-mode: forwards
    }
}
.brs-popup{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: 1;
    .brs-popup__mask{
        width: 100%;
        height: 100%;
        background-color: #000;
        opacity: .6;
        position: absolute;
        z-index: 1;
        top: 0;
        left: 0;
    }
    .brs-popup__wrapper{
        width: 100%;
        background-color: #ffffff;
        position: absolute;
        bottom: 0;
        left: 0;
        z-index: 2;
        .brs-popup__header{
            width: 100%;
            height: 80px;
            display: flex;
            align-items: center;
            overflow: hidden;
            .brs-popup__cancel, .brs-popup__confirm{
                position: absolute;
                top: 0;
                width: 100px;
                height: 80px;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 20px;
            }
            .brs-popup__cancel{
                left: 0;
                color: #666;
            }
            .brs-popup__confirm{
                right: 0;
            }
            .brs-popup__title{
                margin: 0 100px;
                text-align: center;
                font-size: 26px;
                flex: 1;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
            }
        }
        .brs-popup__body{
            width: 100%;
            min-height: 400px;
        }
    }
}
</style>